package com.yuexunit.account.entity;

import com.yuexunit.account.action.v2.model.Session;
import com.yuexunit.persist.annotation.Cacheable;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.UuidUtil;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import java.util.Date;


/**
 * 帐号会话 
 * @author wangjinshui
 */
@Entity
@Table(name="AC_ACCOUNT_SESSION")
@Cacheable
public class AccountSessionEntity {
	@Id
	@Column(name="SESSION_UUID")
	private String sessionUuid;

	@Column(name="ACCOUNT_ID")
	private Long accountId;

	@Column(name="APP_UUID")
	private String appUuid;
	
	@Column(name="APP_ROLE_TYPE")
	private Integer appRoleType;
	
	@Column(name="EXPIRE_DT")
	private Date expireDate;

	@Column(name="CREATE_DT")
	private Date createDate;

	
	public AccountSessionEntity() {
	}
	
	public AccountSessionEntity(Long accountId, String appUuid, Integer appRoleType) {
        initSessionUuid();
		setAccountId(accountId);
		setAppUuid(appUuid);
		setAppRoleType(appRoleType);
        initExpireDate();
        initCreateDate();
	}
	
	
	public String getSessionUuid() {
		return sessionUuid;
	}

	public void setSessionUuid(String sessionUuid) {
		this.sessionUuid = sessionUuid;
	}

	public Long getAccountId() {
		return accountId;
	}

	public void setAccountId(Long accountId) {
		this.accountId = accountId;
	}
	
	public String getAppUuid() {
		return appUuid;
	}

	public void setAppUuid(String appUuid) {
		this.appUuid = appUuid;
	}

	public Integer getAppRoleType() {
		return appRoleType;
	}

	public void setAppRoleType(Integer appRoleType) {
		this.appRoleType = appRoleType;
	}

	public Date getExpireDate() {
		return expireDate;
	}

	public void setExpireDate(Date expireDate) {
		this.expireDate = expireDate;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	
	/**
	 * 设置过期时间为十年后的当前时间。
	 */
	public void initExpireDate() {
		setExpireDate(DateUtil.tenYearsLater());
	}
	
	/**
	 * 设置创建时间为当前时间。
	 */
	public void initCreateDate() {
		setCreateDate(DateUtil.now());
	}
	
	/**
	 * @return 过期时间早于系统当前时间，则返回true；否则返回false。
	 */
	public boolean expired() {
		return getExpireDate() == null ? true : getExpireDate().before(DateUtil.now());
	}
	
	/**
	 * 随机生成UUID，并设置为Session Uuid
	 */
	public void initSessionUuid() {
		setSessionUuid(UuidUtil.randomUUID());
	}
	
	
	/**
	 * @return 会话信息
	 */
	public Session toSession() {
		Session session = new Session();
		session.setSessionUuid(getSessionUuid());
		session.setAccountId(getAccountId());
		session.setAppRoleType(getAppRoleType());
		return session;
	}
	
	
	@Override
	public String toString() {
		StringBuffer result = new StringBuffer();
		
		result.append(getClass().getName())
			.append(": sessionUuid='").append(sessionUuid).append("'")
			.append(", accountId=").append(accountId)
			.append(", appUuid='").append(appUuid).append("'")
			.append(", appRoleType=").append(appRoleType)
			.append(", expireDate=").append(expireDate)
			.append(", createDate=").append(createDate)
			;
		
		return result.toString();
	}
}
